package com.tanhua.sso.service;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.tanhua.sso.config.AliyunSMSConfig;
import com.tanhua.sso.vo.ErrorResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.time.Duration;

@Service
@Slf4j
public class SmsService {
    @Autowired
    private AliyunSMSConfig aliyunSMSConfig;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    public String sendSms(String mobile) {
        DefaultProfile profile = DefaultProfile.getProfile(aliyunSMSConfig.getRegionId(),
                aliyunSMSConfig.getAccessKeyId(), aliyunSMSConfig.getAccessKeySecret());
        IAcsClient client = new DefaultAcsClient(profile);

        String code = RandomUtils.nextInt(100000, 999999) + "";

        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain(aliyunSMSConfig.getDomain());
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        request.putQueryParameter("RegionId", aliyunSMSConfig.getRegionId());
        request.putQueryParameter("PhoneNumbers", mobile);
        request.putQueryParameter("SignName", aliyunSMSConfig.getSignName());
        request.putQueryParameter("TemplateCode", aliyunSMSConfig.getTemplateCode());
        request.putQueryParameter("TemplateParam", "{\"code\":\""+code+"\"}");
        try {
            CommonResponse response = client.getCommonResponse(request);
            String data = response.getData();
            if (StringUtils.contains(data,"\"Message\":\"OK\"")){
                return code;
            }
            log.info("发送短信验证码失败~ data = "+data);
        } catch (Exception e) {
           log.error("发送短信验证码失败~ mobile = "+ mobile,e);
        }
        return null;
    }

    public ErrorResult sendCheckCode(String phone){
        String atk ="CHECK_CODE_"+phone;
        if (redisTemplate.hasKey(atk)){
            String msg = "上次验证码还未失效";
            return ErrorResult.builder().errCode("000001").errMessage(msg).build();
        }

        //String code = sendSms(phone);
        String code = "123456";
        if (StringUtils.isEmpty(code)){
            String msg = "发送验证码错误";
            return ErrorResult.builder().errCode("000000").errMessage(msg).build();
        }

        this.redisTemplate.opsForValue().set(atk,code,Duration.ofMillis(5));

        return null;
    }

    public void test() {
        String phone= "17602026868";
        String atk ="CHECK_CODE_"+phone;
        String code = "123456";

        this.redisTemplate.opsForValue().set(atk,code,Duration.ofMinutes(10));
    }

    public String testGet(){
        String phone= "17602026868";
        String atk ="CHECK_CODE_"+phone;
        String s = this.redisTemplate.opsForValue().get(atk);
        System.out.println(this.redisTemplate.hasKey(atk));
        return s;
    }
}